package Game;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class Teclado {

    private int x = 0;
    private Image img, der, izq;
    private final int diametro = 50;
    private final Game game;

    public Teclado(Game game) {
        this.game = game;
        String img1 = "images/nave.png";
        URL imgUrl = getClass().getClassLoader().getResource(img1);
        if (imgUrl == null) {
            System.err.println("Couldn't find file: " + img1);
        } else {
            try {
                img = ImageIO.read(imgUrl);
            } catch (IOException ex) {
            }
        }
        img1 = "images/der1.png";
        imgUrl = getClass().getClassLoader().getResource(img1);
        if (imgUrl == null) {
            System.err.println("Couldn't find file: " + img1);
        } else {
            try {
                der = ImageIO.read(imgUrl);
            } catch (IOException ex) {
            }
        }
        img1 = "images/izq1.png";
        imgUrl = getClass().getClassLoader().getResource(img1);
        if (imgUrl == null) {
            System.err.println("Couldn't find file: " + img1);
        } else {
            try {
                izq = ImageIO.read(imgUrl);
            } catch (IOException ex) {
            }
        }
    }

    public void choque() {
        String img1 = "images/expl.png";
        URL imgUrl = getClass().getClassLoader().getResource(img1);
        if (imgUrl == null) {
            System.err.println("Couldn't find file: " + img1);
        } else {
            try {
                img = ImageIO.read(imgUrl);
            } catch (IOException ex) {
            }
        }
    }

    public void res() {
        String img1 = "images/nave.png";
        URL imgUrl = getClass().getClassLoader().getResource(img1);
        if (imgUrl == null) {
            System.err.println("Couldn't find file: " + img1);
        } else {
            try {
                img = ImageIO.read(imgUrl);
            } catch (IOException ex) {
            }
        }
    }

    public void pintar(Graphics2D g) {
        g.drawImage(img, 50 + x, this.game.getHeight() - diametro, diametro,
                diametro, null);
        g.drawImage(izq, 170, 250, diametro, diametro, null);
        g.drawImage(der, 230, 250, diametro, diametro, null);
    }

    public void TecLib(KeyEvent e) {
        this.x = 0;
    }

    public void TecPres(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_A) {
            this.x = -50;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            this.x = 50;
        }
        if (e.getKeyCode() == KeyEvent.VK_P) {
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            this.x = -50;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            this.x = 50;
        }
    }

    public Rectangle getBounds() {
        return new Rectangle(50 + x, this.game.getHeight() - diametro,
                diametro, diametro);
    }
}
